// pages/main/Policy.js
import api from "../../utils/api.js";
import wechat from '../../utils/wechat.js';
import network from '../../utils/network.js';
import formatDate from "../../utils/format_date.js";
import notification from '../../utils/notification_center.js';

let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    finish: true,
    tab1: {
      list: [{
        id: 0,
        title: "热点政策",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 1,
        title: "最新政策",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 2,
        title: "科技新闻",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 3,
        title: "科技动态",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }],
      selectedId: 0,
      scroll: true
    },
    tab2: {
      list: [{
        id: 0,
        title: "国家政策法规",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 1,
        title: "省政策法规",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 2,
        title: "部门规范文件",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 3,
        title: "政策解读",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }],
      selectedId: 0,
      scroll: true
    },
    tab3: {
      list: [{
        id: 0,
        title: "国务院",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 1,
        title: "财政部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 2,
        title: "科技部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 3,
        title: "商务部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 4,
        title: "税务总局",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 5,
        title: "工信部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 6,
        title: "发改委",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 7,
        title: "农业部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 8,
        title: "银监部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 9,
        title: "知识产权局",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 10,
        title: "工商总部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 11,
        title: "质检总局",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 12,
        title: "交通部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 13,
        title: "林业部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 14,
        title: "水利部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 15,
        title: "教育部",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 16,
        title: "自然科学纪委会",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }],
      selectedId: 0,
      scroll: true
    },
    tab4: {
      list: [{
        id: 0,
        title: "中央政策",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 1,
        title: "湖北省政策",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 2,
        title: "荆州市政策",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }, {
        id: 3,
        title: "公安县政策",
        page: 1,
        nowPage: 0,
        maxPage: 1,
        details: []
      }],
      selectedId: 0,
      scroll: true
    },
    activityIndex: 0,
    url: app.globalData.url
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // if (options.index == 1) {
    //   this.setData({
    //     tab: this.data.tab1
    //   })
    // } else if (options.index == 2) {
    //   this.setData({
    //     tab: this.data.tab2
    //   })
    // } else if (options.index == 3) {
    //   this.setData({
    //     tab: this.data.tab3
    //   })
    // } else if (options.index == 5) {
      this.setData({
        tab: this.data.tab4
      })
    // }

    // this.setData({
    //   'activityIndex': options.page,
    //   'tab.selectedId': this.data.tab.list[options.page].id
    // })

    // notification.addNotification("RefreshOrder", this.refreshNotification, this);
    this.toast = this.selectComponent("#toast");
    this.onPullDownRefresh();
  },

  // /**
  //  * 生命周期函数--监听页面初次渲染完成
  //  */
  // onReady: function() {

  // },

  // /**
  //  * 生命周期函数--监听页面显示
  //  */
  onShow: function() {
    let dbAllNum = wx.getStorageSync('dbAllNum')
    if(dbAllNum>0){
      wx.setTabBarBadge({
        index: 2,
        text: dbAllNum.toString()
      })
    }

  },

  // /**
  //  * 生命周期函数--监听页面隐藏
  //  */
  // onHide: function() {

  // },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // notification.removeNotification("RefreshOrder", this);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.setData({
      [`tab.list[${this.data.activityIndex}].page`]: 1,
      [`tab.list[${this.data.activityIndex}].nowPage`]: 0,
      [`tab.list[${this.data.activityIndex}].maxPage`]: 1,
      [`tab.list[${this.data.activityIndex}].details`]: []
    })
    this.request();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.request();
  },

  /**
   * 请求数据
   * 将每一页的数据放一个数组，即形成一个二维数组,减少页面渲染的次数
   */
  request: function() {
    let item = this.data.tab.list[this.data.activityIndex]
    if (item.nowPage == item.maxPage) {
      this.toast.showToast("暂无更多");
    } else {
      network.postJson(this, api.zhengceList, {
        page: item.page,
        type: this.data.tab.list[this.data.activityIndex].title,
      }).then(res => {
        for (let i = 0; i < res.data.data.rows.length; i++) {
          res.data.data.rows[i].ids = res.data.data.rows[i].id;
          res.data.data.rows[i].ltitle = res.data.data.rows[i].title;
          res.data.data.rows[i].id = res.data.data.rows[i].id;
        }
        this.setData({
          [`tab.list[${this.data.activityIndex}].details[${item.nowPage}]`]: res.data.data.rows,
          [`tab.list[${this.data.activityIndex}].page`]: res.data.data.nextpage
        })
        this.data.tab.list[this.data.activityIndex].nowPage = res.data.data.page;
        this.data.tab.list[this.data.activityIndex].maxPage = res.data.data.maxpage;

      })
    }
  },

  /**
   * 点击tab
   */
  tabTapChange: function(e) {
    this.setData({
      activityIndex: e.detail.index,
      'tab.selectedId': e.detail.selectedId
    })
    this.onPullDownRefresh();
  },

  goPolicyDetails(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/policy/policy/PolicyDetails?id=' + id
    })
  }
})